<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class ApiController extends Controller
{

    public function __construct()
    {
    }

    public function anyNotify(Request $request)
    {
        \Log::info(date("Y-m-d H:i:s") . ":" . json_encode($request->all()));
        // 判断是否有合法的事件信息包含
        // event = 事件（ringing，answer，hangup 等）
        // source = 来源（agent，callee）
        // calltype = 呼叫类型（dialin，dialout）

        $event = $request->input("event");
        $source = $request->input("source");
        $calltype = $request->input("calltype");

        $user = User::where("phone", $request->input("calleridnum"))->first();

        if ($user) {
            $user->status = 1;
            $user->called_at = date("Y-m-d H:i:s");
            $user->save();

        }

        return $this->successResponse();

    }

    public function getTest()
    {
        return view("tt");
    }

    public function getUserStatus()
    {
        $user = User::where("status", 1)->orderBy("called_at")->first();

        if ($user) {
            $user->status = 0;
            $user->save();
            return $this->successResponse($user);

        } else {
            return $this->failResponse("无新消息");
        }


    }
}
